package com.tabbinder.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;

/**
 * Common portion of various response handlers: ignore not-logged-in and pop up
 * alert window on errors
 * 
 * @author Tatiana Kichkaylo
 */
public abstract class CommonResponseHandler implements RequestCallback {
	public void onError(Request request, Throwable exception) {
		Window.alert("Error " + exception);
	}

	public void onResponseReceived(Request request, Response response) {
		if (Constants.STATUS_CODE_OK == response.getStatusCode()) {
			processResponseText(response.getText());
		} else if (Constants.STATUS_CODE_UNAUTHORIZED == response
				.getStatusCode()) {
			// login required - ignore here, will take care in MasterBind
		} else {
			// handle other non-OK response from the server
			Window.alert("Result: " + response.getStatusCode() + " "
					+ response.getStatusText());
		}
	}

	protected abstract void processResponseText(String text);
}
